/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.memberView;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TSendCardRecord;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.service.TUserService;
import com.ruipengkj.view.base.BasePanel;
/**
 * 说明：
 * 发 卡UI
 * @Author：黄盛强
 * @Date：2012-03-20
 */
    public class AddUserPanel extends BasePanel  {
	
	
	private static final long serialVersionUID = 1L;
	private JLabelJTextFieldComponent number = null;//会员编号
	private JLabelJTextFieldComponent cardNumTF = null;///会员卡号
	private JLabelJTextFieldComponent nameTF = null; //姓名
	private JLabelJTextFieldComponent mobileTF = null; //手机号码
	private JLabelJTextFieldComponent initMoneyTF = null;//初始金额
	//---------------------------------------
	private JLabelJTextFieldComponent emailTF = null;//电子邮箱
	public boolean isSuccess=false;
	private TUser tuer=null;
	
	private JButton sureButton = null; //确定
	private JButton cancelButton = null; //取消
	
	private TUserService tUserService;
	
	
	
	
	
	public AddUserPanel() {
		tUserService=BuildSpringApplication.getInstance().getTUserServiceImpl();
		this.addComponents();
		this.initListener();
		//读卡
//		ApplicationContext.CardUtilThread.setFlag(false);//停止之前线程
//		ApplicationContext.CardUtilThread=new CardUtil(){//重新定义线程
//			protected void send(String drive){
//				cardNumTF.getText().setText(drive);
//				cardNumTF.requestFocus();
//			}
//		};
//		ApplicationContext.CardUtilThread.startOn(cardNumTF.getText());//启动线程
	}
	
	private void addComponents() {
		JPanel main_jp=new JPanel();
		main_jp.setBorder(BorderFactory.createTitledBorder("增加用户"));
		BoxLayout boxLayout=new BoxLayout(main_jp, BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		
		FlowLayout flowLayout=new FlowLayout(FlowLayout.LEFT);
		JPanel cardNum_jp=new JPanel(flowLayout);
		cardNumTF = new JLabelJTextFieldComponent("  *", "卡    号：",30);
		cardNum_jp.add(cardNumTF);
		number = new JLabelJTextFieldComponent("   *", "用户编号：",30);
		number.getText().setText(UUID.GenUUID15());
		number.getText().setEditable(false);
		nameTF = new JLabelJTextFieldComponent("   *", "会员名称：", 30);
		//------------------------------
		emailTF = new JLabelJTextFieldComponent("   *", "电子邮箱：", 30);
		mobileTF= new JLabelJTextFieldComponent("   *", "手机号码：", 30);
		initMoneyTF = new JLabelJTextFieldComponent("   *", "初始金额：", 30);
		initMoneyTF.getText().setText("0.00");
		
		JPanel jp6 = new JPanel(flowLayout);
		sureButton = new JButton("确定(Ctrl+Y)");
		cancelButton = new JButton("取消");
		jp6.add(sureButton);
		jp6.add(cancelButton);
		main_jp.add(number);
		main_jp.add(cardNum_jp);
		main_jp.add(nameTF);
		main_jp.add(emailTF);
		main_jp.add(mobileTF);
		main_jp.add(initMoneyTF);
		main_jp.add(jp6);
		this.add(main_jp);
	}
	
	private void initListener(){
		sureButton.addActionListener(this);
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	

	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource()==sureButton){
			//提交
			String name=nameTF.getText().getText().trim();//会员姓名
			//---------------------------------------------------------------------
			String email=emailTF.getText().getText().trim();
			String cardNum=cardNumTF.getText().getText().trim();//卡号
			String mobile=mobileTF.getText().getText().trim(); //手机号码
			String initMoney=initMoneyTF.getText().getText().trim();//初始金额

			if(cardNum.equals("")||cardNum.length()>15){
				JOptionPane.showMessageDialog(null, "卡号不能为空，且不能大于15个字");
				cardNumTF.getText().requestFocus();
				return;
			}
			
			if(name.equals("")||name.length()>20){
				JOptionPane.showMessageDialog(null, "会员名称不能为空，且不能大于20个字");
				nameTF.getText().requestFocus();
				return;
			}
			//--------------------------------------------------
			if(email.equals("")||name.length()>20){
				JOptionPane.showMessageDialog(null, "电子邮箱不能为空，且不能大于20个字");
				nameTF.getText().requestFocus();
				return;
			}
			if(mobile.equals("")||!MatcherFormatUtil.checkMobileScheme(mobile)){
				JOptionPane.showMessageDialog(null, "手机号码不能为空，请输入正确的手机号码");
				mobileTF.getText().requestFocus();
				return;
			}
			if(initMoney.equals("")||!MatcherFormatUtil.MatcherPriceScheme(initMoney)){
				JOptionPane.showMessageDialog(null, "初始金额不能为空，请确认格式是否正确");
				initMoneyTF.getText().requestFocus();
				return;
			}
			Date date=new Date();
			TSendCardRecord tSendCardRecord=new TSendCardRecord();
			tSendCardRecord.setSendCardRecordID(UUID.GenUUID15());
			tSendCardRecord.setCardNum(cardNum);
			tSendCardRecord.setSendCardTime(date);
			TUser tUser=new TUser();
			tUser.setUserID(number.getText().getText().trim());
			tUser.setRealName(name);
			tUser.setEMail(email);
			tUser.setMobile(mobile);
			tUser.setMoney(Double.parseDouble(initMoney));
			tUser.setCreateTime(date);
			tUser.setLastLoginTime(date);
			tUser.setSendCardTime(date);
			tUser.setCardNum(cardNum);
			tUser.getTSendCardRecords().add(tSendCardRecord);
			try {
				if(tUserService.getByMobileService(mobile)!=null){
					JOptionPane.showMessageDialog(null, "该手机号码已经存在，请重新输入");
					mobileTF.getText().requestFocus();
					return;
				}
				if(tUserService.getByCardNumService(cardNumTF.getText().getText().trim())!=null){
					JOptionPane.showMessageDialog(null, "该卡号已经存在，请重新输入");
					cardNumTF.getText().requestFocus();
					return;
				}
				tUserService.saveService(tUser);
				BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("会员发卡，卡号为：+"+cardNumTF.getText().getText().trim());
				JOptionPane.showMessageDialog(null, "操作成功");
				this.clear();
			} catch (Exception e1) {
				e1.printStackTrace();
				JOptionPane.showMessageDialog(null, "操作失败，请重新操作");
				this.clear();
			}
		}
	}
	
	//-------------------------------------------------- 修改用户的弹出框
	
	public void showDialog(TUser tuser){
		this.isSuccess=false;
		if(tuser!=null){
			//this.WeightingPrice.getText().setText(ApplicationContext.WeightingPrice(tCommodity)+"");
			this.tuer=tuser;
			
		   cardNumTF.getText().setText(tuser.getCardNum()); ///会员卡号
		   nameTF.getText().setText(tuser.getUserName());
		   mobileTF.getText().setText(tuser.getMobile());
		   initMoneyTF.getText().setText(tuser.getMoney()+"");
		   emailTF.getText().setText(tuser.getEMail());
			//---------------------------------------
			
		}
		this.setVisible(true);
	}

	@Override
	public void clear() {
		cardNumTF.getText().setText("");//会员卡号
		nameTF.getText().setText(""); //会员姓名
		mobileTF.getText().setText("") ; //手机号码
		initMoneyTF .getText().setText("");//初始金额
		//--------------------------------------------------------------------
		emailTF.getText().setText("");     //  电子邮箱
		cardNumTF.getText().requestFocus();
	}

	
}
